package com.redpacketcover.rc.base;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;


import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

public class BasicMode extends ViewModel {

    private CompositeDisposable mCompositeDisposable;


    protected <T> T getService(Class<T> cls) {
        return RetrofitManager.getInstance().create(cls);
    }


    //RxJava取消注册，以避免内存泄露
    public void onUnSubscribe() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.clear();
            mCompositeDisposable = null;
        }

    }

    /**
     * 开始请求
     */
    protected <T> void getRequestData(Observable<BaseResponseBean<T>> observable, final MutableLiveData liveData) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        DisposableObserver observer = new BasicObserver<T>(liveData);
        mCompositeDisposable.add(observer);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }


}
